package com.ctrip.cap.common.shell;

import static com.ctrip.cap.common.shell.OS.platformExecutableSuffixBat;
import static com.ctrip.cap.common.shell.OS.platformExecutableSuffixExe;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;

import com.ctrip.cap.common.shell.exception.AndroidSdkException;

public class AndroidSdk {

	public static final String ANDROID_FOLDER_PREFIX = "android-";
	public static final String ANDROID_HOME = "ANDROID_HOME";

	public static File adb() {

		return new File(platformToolsHome(), "adb"
				+ platformExecutableSuffixExe());
	}

	public static File aapt() throws AndroidSdkException {
		StringBuffer command = new StringBuffer();
		command.append("aapt");
		command.append(platformExecutableSuffixExe());
		File platformToolsAapt = new File(platformToolsHome(),
				command.toString());

		if (platformToolsAapt.isFile()) {
			return platformToolsAapt;
		}

		File buildToolsFolder = buildToolsHome();

		return new File(
				findLatestAndroidPlatformFolder(
						buildToolsFolder,
						"Command 'aapt' was not found inside the Android SDK. Please update to the latest development tools and try again."),
				command.toString());
	}

	public static File android() {
		StringBuffer command = new StringBuffer();
		command.append(toolsHome());

		return new File(toolsHome(), "android" + platformExecutableSuffixBat());
	}

	public static File emulator() {
		return new File(toolsHome(), "emulator" + platformExecutableSuffixExe());
	}

	private static File toolsHome() {
		StringBuffer command = new StringBuffer();
		command.append(androidHome());
		command.append(File.separator);
		command.append("tools");
		command.append(File.separator);
		return new File(command.toString());
	}

	private static File buildToolsHome() {
		StringBuffer command = new StringBuffer();
		command.append(androidHome());
		command.append(File.separator);
		command.append("build-tools");
		command.append(File.separator);

		return new File(command.toString());
	}

	private static File platformToolsHome() {
		StringBuffer command = new StringBuffer();
		command.append(androidHome());
		command.append(File.separator);
		command.append("platform-tools");
		command.append(File.separator);
		return new File(command.toString());
	}

	public static String androidHome() {
		String androidHome = System.getenv(ANDROID_HOME);

		if (androidHome == null) {
			throw new RuntimeException("Environment variable '" + ANDROID_HOME
					+ "' was not found!");
		}
		return androidHome;
	}

	/**
	 * @return path to android.jar of latest android api.
	 */
	public static String androidJar() {
		String platformsRootFolder = androidHome() + File.separator
				+ "platforms";
		File platformsFolder = new File(platformsRootFolder);

		return new File(findLatestAndroidPlatformFolder(platformsFolder,
				"No installed Android APIs have been found."), "android.jar")
				.getAbsolutePath();
	}

	protected static File findLatestAndroidPlatformFolder(File rootFolder,
			String errorMessage) {
		File[] androidApis = rootFolder.listFiles(new AndroidFileFilter());
		if (androidApis == null || androidApis.length == 0) {
			throw new RuntimeException(errorMessage);
		}
		Arrays.sort(androidApis, Collections.reverseOrder());
		return androidApis[0].getAbsoluteFile();
	}

	public static class AndroidFileFilter implements FileFilter {

		@Override
		public boolean accept(File pathname) {
			String fileName = pathname.getName();

			String regex = "\\d{2}\\.\\d{1}\\.\\d{1}";
			if (fileName.matches(regex)
					|| fileName.startsWith(ANDROID_FOLDER_PREFIX)) {
				return true;
			}
			return false;
		}
	}
}
